import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:znmm/UI/A_Common/buttons/easy_button/easy_elevated_button.dart';
import 'package:znmm/UI/A_Common/input_item.dart';
import 'package:znmm/UI/utils/my_global.dart';
import 'package:znmm/config/app_color.dart';
import 'package:znmm/config/app_gaps.dart';
import '../mag/edti_temp_mag.dart';
import 'sel_language_view.dart';
//import '../../message_management/template_edti/template_edit_mag.dart';

class AiTempView extends StatefulWidget {
  const AiTempView({super.key});

  @override
  State<AiTempView> createState() => _AiTempDialogState();
}

class _AiTempDialogState extends State<AiTempView> {
  final TextEditingController _companyNameTEC = TextEditingController();
  final TextEditingController _productInfoTEC = TextEditingController();

  //公司优势输入
  final TextEditingController _companyAdvantageTEC = TextEditingController();

  //产品优势输入
  final TextEditingController _productAdvantageTEC = TextEditingController();
  //服务优势输入
  final TextEditingController _serviceAdvantageTEC = TextEditingController();
  //行动号召输入
  final TextEditingController _callToActionTEC = TextEditingController();

  //语言
  String _outputlanguage = 'en';

  //当前选择的语言风格
  String _selectLanguageStyle = 'professional';

  ///选择邮件长度
  String _selectEmailLength = 'short';

  ///选择的使用场景
  String _selectUseScene = 'promotional';

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    _companyNameTEC.dispose();
    _productInfoTEC.dispose();

    _companyAdvantageTEC.dispose();
    _productAdvantageTEC.dispose();
    _serviceAdvantageTEC.dispose();
    _callToActionTEC.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: MyGlobal.getNavigationBar(context, "智能生成"),
        body: Center(
            child: Container(
          decoration: BoxDecoration(
            color: AppColor.white,
            borderRadius: BorderRadius.circular(10),
          ),
          child: Column(
            children: [
              Expanded(
                child: SingleChildScrollView(
                  child: Padding(
                    padding: EdgeInsets.symmetric(horizontal: 10.w),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        ///公司名称
                        InputItem(
                          title: '公司名',
                          hintText: '选填,填写公司名称',
                          controller: _companyNameTEC,
                          keyboardType: TextInputType.text,
                          showArrow: false,
                          maxLine: 1,
                        ),

                        ///产品
                        InputItem(
                          title: '公司产品',
                          hintText: '选填,填写产品',
                          controller: _productInfoTEC,
                          keyboardType: TextInputType.text,
                          showArrow: false,
                          maxLine: 1,
                        ),
                        Text(
                          '语言',
                          style: TextStyle(
                            color: AppColor.black,
                            fontSize: 16.sp,
                          ),
                        ),
                        AppGaps.vGaps10w,
                        _languageSelectItem(),

                        ///选择语言风格
                        Text(
                          "语言风格",
                          style: TextStyle(
                            color: AppColor.black,
                            fontSize: 16.sp,
                          ),
                        ),
                        AppGaps.vGaps10w,

                        CheckItem(
                          list: EdtiTempMag().languageStyleList,
                          onTap: (String data) {
                            setState(() {
                              _selectLanguageStyle = data;
                            });
                          },
                          value: _selectLanguageStyle,
                        ),
                        Text(
                          "邮件长度",
                          style: TextStyle(
                            color: AppColor.black,
                            fontSize: 16.sp,
                          ),
                        ),
                        AppGaps.vGaps10w,

                        ///横向单选功能
                        CheckItem(
                          list: EdtiTempMag().emailList,
                          onTap: (String data) {
                            setState(() {
                              _selectEmailLength = data;
                            });
                          },
                          value: _selectEmailLength,
                        ),
                        Text(
                          "描述使用场景",
                          style: TextStyle(
                            color: AppColor.black,
                            fontSize: 16.sp,
                          ),
                        ),
                        AppGaps.vGaps10w,

                        ///横向单选功能
                        CheckItem(
                          list: EdtiTempMag().useSceneList,
                          onTap: (String data) {
                            setState(() {
                              _selectUseScene = data;
                            });
                          },
                          value: _selectUseScene,
                        ),

                        ///公司优势
                        InputItem(
                          title: '公司优势',
                          hintText: '选填,填写公司优势',
                          controller: _companyAdvantageTEC,
                          keyboardType: TextInputType.text,
                          maxLine: 3,
                          showArrow: false,
                        ),

                        ///产品优势
                        InputItem(
                          title: '产品优势',
                          hintText: '选填,填写产品优势',
                          controller: _productAdvantageTEC,
                          keyboardType: TextInputType.text,
                          maxLine: 3,
                          showArrow: false,
                        ),
                        //服务优势
                        InputItem(
                          title: '服务优势',
                          hintText: '选填,填写服务优势',
                          controller: _serviceAdvantageTEC,
                          keyboardType: TextInputType.text,
                          maxLine: 3,
                          showArrow: false,
                        ),

                        ///行动号召
                        InputItem(
                          title: '行动号召',
                          hintText: '选填,填写行动号召',
                          controller: _callToActionTEC,
                          keyboardType: TextInputType.text,
                          maxLine: 3,
                          showArrow: false,
                        ),
                      ],
                    ),
                  ),
                ),
              ),
              const SizedBox(
                height: 15,
              ),
              EasyElevatedButton(
                label: '生成',
                //color: Colors.blue,
                onPressed: () {
                  EdtiTempMag().aiAutoTemplate(
                    _companyNameTEC.text,
                    _productInfoTEC.text,
                    _companyAdvantageTEC.text,
                    _productAdvantageTEC.text,
                    _serviceAdvantageTEC.text,
                    _callToActionTEC.text,
                    _outputlanguage,
                    _selectLanguageStyle,
                    _selectEmailLength,
                    _selectUseScene,
                    callback: (po) {
                      Navigator.maybePop(context);
                    },
                  );
                },
              ),
              const SizedBox(
                height: 44,
              ),
            ],
          ),
        )));
  }

  ///语言选择框
  Widget _languageSelectItem() {
    return GestureDetector(
      onTap: () {
        Get.bottomSheet(SelLanguageView(
          onLanguageSelected: (String value) {
            if (mounted) {
              setState(() {
                _outputlanguage = value;
              });
            }
          },
        ));
      },
      //下拉多选国家地区
      child: SelectItem(
        hintText: '请选择语言',
        value: 1 == 1
            ? EdtiTempMag().languageList[EdtiTempMag()
                    .languageList
                    .indexWhere((element) => element['id'] == _outputlanguage)]
                ['en']
            : EdtiTempMag().languageList[EdtiTempMag()
                    .languageList
                    .indexWhere((element) => element['id'] == _outputlanguage)]
                ['zh'],
      ),
    );
  }
}

/////--------------------------------------

typedef ValueCallback<T> = void Function(T value);

class CheckItem<T> extends StatelessWidget {
  const CheckItem({
    super.key,
    required this.list,
    required this.onTap,
    required this.value,
  });
  final List list;
  //带返回值的点击事件
  final ValueCallback<T> onTap;

  final String value;

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        ...List.generate(
            list.length,
            (index) => Expanded(
                  child: Tooltip(
                    message: 1 == 1 ? list[index]['en'] : list[index]['zh'],
                    child: GestureDetector(
                      onTap: () {
                        onTap(list[index]['id']);
                      },
                      child: Container(
                        padding: EdgeInsets.symmetric(
                            horizontal: 2.w, vertical: 3.h),
                        decoration: BoxDecoration(
                          border: Border.all(
                            color: value == list[index]['id']
                                ? AppColor.blue396EC6
                                : AppColor.greyD8D8D8,
                          ),
                        ),
                        alignment: Alignment.center,
                        child:
                            Text(1 == 1 ? list[index]['en'] : list[index]['zh'],
                                maxLines: 1,
                                style: TextStyle(
                                  fontSize: 14.sp,
                                  overflow: TextOverflow.ellipsis,
                                  color: value == list[index]['id']
                                      ? AppColor.blue396EC6
                                      : AppColor.greyD8D8D8,
                                )),
                      ),
                    ),
                  ),
                ))
      ],
    );
  }
}

/////--------------------------------------
class SelectItem extends StatelessWidget {
  final String hintText;
  final String? value;
  final VoidCallback? onTap;

  ///max height
  final double? maxHeight;

  const SelectItem({
    Key? key,
    required this.hintText,
    this.value,
    this.onTap,
    this.maxHeight = 50,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            width: double.infinity,
            constraints: BoxConstraints(
              maxHeight: maxHeight!,
            ),
            padding: EdgeInsets.all(10.w),
            decoration: BoxDecoration(
              color: AppColor.greyF3F4F8,
              borderRadius: BorderRadius.circular(10.w),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(
                  child: Text(
                    value ?? hintText,
                    //溢出省略
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 16.sp,
                      color: value == null ? AppColor.grey8080 : AppColor.black,
                    ),
                  ),
                ),
                Icon(
                  Icons.keyboard_arrow_down_rounded,
                  size: 25.w,
                  color: AppColor.black,
                ),
              ],
            ),
          ),
          AppGaps.vGaps10w,
        ],
      ),
    );
  }
}
